package chixing.day17.homework.work4;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class EmployTest {
    static List<Employee> employeeList = new ArrayList<>();

    public static void main(String[] args) {
        String filePath = "E:\\2025-cx\\project\\PJ\\J01\\Javase\\Temp\\salaries.txt";
        getEmployeeList(filePath);
        employeeList.forEach(System.out::println);
        getHeightSalary();
    }

    /**
     * 将文件中的数据读取出来并添加到列表中
     *
     * @param filePath 要读取的文件的路径
     */
    public static void getEmployeeList(String filePath) {
        //创建一个读取流
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        //一行一行读
        String title = null;
        try {//第一行标题行不要
            title = bufferedReader.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String line = null;
        try {
            while ((line = bufferedReader.readLine()) != null) {
                //对读出的一行数据进行分割
                String[] split = line.split("\\|");
                //转换成Employee类的相对应的数据类型
                int empId = Integer.valueOf(split[0]);
                String empName = split[1];
                double empSalary = Double.valueOf(split[2]);
                Employee employee = new Employee(empId, empName, empSalary);
                employeeList.add(employee);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
    /**
     * 将工资高于 20000 的员工数据写入新文件 high_salary_report.txt。
     */
    public static void getHeightSalary() {
        BufferedWriter bufferedWriter = null;
        //创建一个写入流
        try {
            bufferedWriter=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("E:\\2025-cx\\project\\PJ\\J01\\Javase\\Temp\\high_salary_report.txt")));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //循环列表中的内容，判断薪资是否大于20000，如果是写入
        try {
            for (Employee employee : employeeList) {
                if (employee.getEmpSalary() > 20000) {//写入
                    bufferedWriter.write(employee.getEmpId() + "-" + employee.getEmpName() + "-" + employee.getEmpSalary() + "\n");
                    bufferedWriter.flush();
                }
            }
        }
         catch (IOException e) {
            throw new RuntimeException(e);
        } finally {//关闭流
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

}
